<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="银行名称" prop="bankName">
        <el-input
          v-model="queryParams.bankName"
          placeholder="请输入银行名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-input
          v-model="queryParams.createTime"
          placeholder="请输入创建时间"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['visitcorp:CLOCK:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          @click="handleUpdate"
          v-hasPermi="['visitcorp:CLOCK:edit']"
          >修改</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['visitcorp:CLOCK:remove']"
          >删除</el-button
        >
      </el-col>
      -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['visitcorp:CLOCK:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="listApiBankUser">
      <el-table-column label="选择" align="center" width="65">
        <template scope="scope">
          <el-radio
            :label="scope.$index"
            v-model="selectRadio"
            @change.native="getCurrentRow(scope.row)"
            ><div></div
          ></el-radio>
        </template>
      </el-table-column>
      <el-table-column label="银行名称" align="center" prop="bankName" />
      <el-table-column label="是否可查询非授权户" align="center" prop="isAuth">
        <template slot-scope="scope">
          <div :class="scope.row.isAuth === 0 ? 'isAuthFalse' : 'isAuthTrue'">
            {{ scope.row.isAuth === 0 ? "否" : "是" }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="授权户是否计数" align="center" prop="isAuthCount">
        <template slot-scope="scope">
          <div
            :class="scope.row.isAuthCount === 0 ? 'isAuthFalse' : 'isAuthTrue'"
          >
            {{ scope.row.isAuthCount === 0 ? "否" : "是" }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="初始查询次数" align="center" prop="initAuthCount">
        <template slot-scope="scope">
          {{ scope.row.initAuthCount === -1 ? "-" : scope.row.initAuthCount }}
        </template>
      </el-table-column>
      <!-- <el-table-column label="剩余查询次数" align="center" prop="authCount" /> -->
      <el-table-column label="剩余查询次数" align="center" prop="authCount">
        <template slot-scope="scope">
          {{ scope.row.initAuthCount === -1 ? "-" : scope.row.authCount }}
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="状态" align="center" prop="authCount">
        <template slot-scope="scope">
          {{
            scope.row.jarStatus === 1
              ? "运行中"
              : scope.row.jarStatus === 0
              ? "未运行"
              : "-"
          }}
        </template>
      </el-table-column>
      <el-table-column
        label="JAR包状态"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <!-- <el-switch
            v-model="scope.row.status"
            active-value="0"
            inactive-value="1"
            @change="handleStatusChange(scope.row)"
          ></el-switch> -->
          <div>
            <div class="loading_icon" v-if="scope.row.loadIshow">
              <img src="@/assets/images/loading_icon.png" alt="light" />
            </div>
            <div v-else>
              <div
                class="kz_icon"
                @click="startBtn(scope.row)"
                v-if="scope.row.jarStatus == 0"
              >
                <img src="@/assets/images/starts_icon.png" alt="light" />
              </div>
              <div
                class="kz_icon2"
                @click="suspendBtn(scope.row)"
                v-if="scope.row.jarStatus == 1"
              >
                <img src="@/assets/images/zanting_icon.png" alt="light" />
              </div>
              <div
                class="kz_icon2"
                @click="suspendBtn(scope.row)"
                v-if="scope.row.jarStatus == 2"
              >
                -
              </div>
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <div
            v-if="
              scope.row.initAuthCount == -1 ||
              scope.row.initAuthCount == scope.row.authCount
            "
            style="font-size:12px;color:rgb(96, 98, 102)"
          >
            暂无记录
          </div>
          <el-button
          v-else
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row, scope.index)"
            v-hasPermi="['monitor:operlog:query']"
            >查询记录</el-button
          >
        </template>
        <!-- <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['visitcorp:CLOCK:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['visitcorp:CLOCK:remove']"
          >删除</el-button>
        </template> -->
      </el-table-column>
    </el-table>

    <!-- <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    /> -->

    <!-- 添加或修改小程序企业走访打卡（银行自己上传功能）对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-form-item label="银行名称" prop="bankName">
          <el-input
            v-model="form.bankName"
            :disabled="bankNamedisabled"
            placeholder="请输入走访ID"
          />
        </el-form-item>
        <el-form-item label="是否可查询非授权户" prop="isAuth">
          <el-radio-group v-model="form.isAuth" :disabled="isAuthdisabled">
            <el-radio :label="1">是</el-radio>
            <el-radio :label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="授权户是否计数" prop="isAuthCount">
          <el-radio-group
            v-model="form.isAuthCount"
            :disabled="isAuthCountdisabled"
          >
            <el-radio :label="1">是</el-radio>
            <el-radio :label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否需要计数">
          <el-switch
            v-model="form.countType"
            @change="countChange"
            :disabled="countTypeDisabled"
          ></el-switch>
        </el-form-item>
        <el-form-item
          label="初始查询次数"
          prop="initAuthCount"
          v-if="form.countType"
        >
          <el-input-number
            v-model="form.initAuthCount"
            :min="1"
            :disabled="initAuthCountdisabled"
            placeholder="请输入初始查询次数"
          />
          <!-- <el-select
            v-model="form.initAuthCount"
            placeholder="请选择"
            :disabled="initAuthCountdisabled"
          >
            <el-option
              v-for="item in initAuthCountList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select> -->
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 添加或修改小程序企业走访打卡（银行自己上传功能）对话框 -->
    <el-dialog
      title="查询记录"
      :visible.sync="open2"
      width="1000px"
      append-to-body
    >
      <el-form
        :model="detailQueryParams"
        ref="detailQueryParams"
        size="small"
        :inline="true"
        label-width="68px"
      >
        <el-form-item label="银行名称" prop="corpName">
          <el-input
            v-model="detailQueryParams.corpName"
            placeholder="请输入银行名称"
            onkeydown="if (event.keyCode===13){return false;}"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="detailHandleQuery"
            >搜索</el-button
          >
        </el-form-item>
      </el-form>
      <el-table v-loading="loading" :data="detailTable">
        <el-table-column label="银行名称" align="center" prop="bankName" />

        <el-table-column label="企业名称" align="center" prop="corpName" />
        <el-table-column
          label="查询时间"
          align="center"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <div>
              {{ scope.row.queryDate.slice(0, 10) }}
            </div>
            <!-- <el-button
              size="mini"
              type="text"
              icon="el-icon-view"
              @click="handleView(scope.row, scope.index)"
              v-hasPermi="['monitor:operlog:query']"
              >查看</el-button
            > -->
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import {
  listApiBankUser,
  addBankUser,
  updateBankUser,
  queryDetail,
  startJar,
  stopJar,
} from "@/api/bankInterface/bankInterface";
import {
  listCLOCK,
  getCLOCK,
  delCLOCK,
  addCLOCK,
  updateCLOCK,
} from "@/api/visitcorp/CLOCK";

export default {
  name: "CLOCK",
  data() {
    return {
      detalBankName: "",
      open2: false,
      countTypeDisabled: false,
      bankNamedisabled: false,
      isAuthdisabled: false,
      isAuthCountdisabled: false,
      initAuthCountdisabled: false,
      checkTrue: {},
      selectRadio: "",
      initAuthCountList: [
        {
          value: -1,
          label: " 不需要计数 ",
        },
        {
          value: 5000,
          label: "5000",
        },
        {
          value: 5,
          label: "5",
        },
      ],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 小程序企业走访打卡（银行自己上传功能）表格数据
      CLOCKList: [],
      //银行列表
      listApiBankUser: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 999999,
        bankName: null,

        userId: null,
        corpCredit: null,
        longitude: null,
        latitude: null,
        clockPlace: null,
      },
      // 表单参数
      form: {
        countType: false,
      },
      detailTable: [],
      detailQueryParams: {
        corpName: "",
      },
      // 表单校验
      rules: {
        bankName: [
          { required: true, message: "请输入银行名称", trigger: "blur" },
        ],
        isAuth: [
          { required: true, message: "请选择活动资源", trigger: "change" },
        ],
        isAuthCount: [
          { required: true, message: "请选择活动资源", trigger: "change" },
        ],
        // initAuthCount: [
        //   { required: true, message: "请选择活动区域", trigger: "change" },
        // ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // handleStatusChange(row) {
    //   console.log(row, "zhuangtai");
    //   if (row == "0") {
    //     this.$prompt("请输入密码", "提示", {
    //       confirmButtonText: "确定",
    //       cancelButtonText: "取消",
    //     }).then(({ value }) => {
    //       if (value == "1") {
    //         this.$message.success("密码正确");

    //         startJar().then((response) => {
    //           console.log(response, "暂停成功");
    //         });
    //       } else {
    //         this.$message.error("密码错误");
    //       }
    //     });
    //   } else {
    //     this.$prompt("请输入密码", "提示", {
    //       confirmButtonText: "确定",
    //       cancelButtonText: "取消",
    //     }).then(({ value }) => {
    //       if (value == "1") {
    //         this.$message.success("密码正确");
    //         stopJar().then((response) => {
    //           console.log(response, "暂停成功");
    //         });
    //       } else {
    //         this.$message.error("密码错误");
    //       }
    //     });
    //   }

    //   // let text = row.status === "0" ? "启用" : "停用";
    //   // this.$modal
    //   //   .confirm('确认要"' + text + '""' + row.userName + '"用户吗？')
    //   //   .then(function () {
    //   //     return changeUserStatus(row.userId, row.status);
    //   //   })
    //   //   .then(() => {
    //   //     this.$modal.msgSuccess(text + "成功");
    //   //   })
    //   //   .catch(function () {
    //   //     row.status = row.status === "0" ? "1" : "0";
    //   //   });
    // },
    suspendBtn(val) {
      //  stopJar().then((response) => {
      //   console.log(response, "暂停成功");
      // });
      let that = this;
      this.$prompt("请输入密码", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      }).then(({ value }) => {
        if (value == "1") {
          this.$message.success("密码正确");
          val.loadIshow = true;
          that.$forceUpdate();
          let listApiBankUserCopy = JSON.parse(
            JSON.stringify(this.listApiBankUser)
          );
          this.listApiBankUser = listApiBankUserCopy;
          stopJar(val.jarPath)
            .then((response) => {
              that.listApiBankUser.forEach((e) => {
                if (e.bankName == val.bankName) {
                  e.jarStatus = 0;
                  e.loadIshow = false;
                }
              });
            })
            .catch((e) => {
              that.listApiBankUser.forEach((e) => {
                if (e.bankName == val.bankName) {
                  e.loadIshow = false;
                }
              });
              this.$message.error("更改失败");
            });
        } else {
          this.$message.error("密码错误");
        }
      });
    },
    startBtn(val) {
      console.log(val);
      let that = this;
      this.$prompt("请输入密码", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      }).then(({ value }) => {
        if (value == "1") {
          this.$message.success("密码正确");
          val.loadIshow = true;
          let listApiBankUserCopy = JSON.parse(
            JSON.stringify(this.listApiBankUser)
          );

          this.listApiBankUser = listApiBankUserCopy;

          startJar(val.jarPath)
            .then((response) => {
              that.listApiBankUser.forEach((e) => {
                if (e.bankName == val.bankName) {
                  e.jarStatus = 1;
                  e.loadIshow = false;
                }
              });
            })
            .catch((e) => {
              that.listApiBankUser.forEach((e) => {
                if (e.bankName == val.bankName) {
                  e.loadIshow = false;
                }
              });
              this.$message.error("更改失败");
            });
        } else {
          this.$message.error("密码错误");
        }
      });
    },
    detailHandleQuery() {
      console.log(this.detailQueryParams, "搜索条件");
      let parmas = {
        bankName: this.detalBankName,
        corpName: this.detailQueryParams.corpName,
      };
      queryDetail(parmas).then((response) => {
        console.log(response, "搜索结果");
        this.detailTable = response.rows;
      });
    },
    getCurrentRow(val) {
      console.log(val, "单选");
      this.checkTrue = val;
    },
    handleView(val) {
      console.log(val, "valval");
      this.detalBankName = val.bankName;
      let parmas = {
        bankName: val.bankName,
      };
      queryDetail(parmas).then((response) => {
        console.log(response, "查看详情");
        this.detailTable = response.rows;
      });
      this.detailQueryParams = {
        corpName: "",
      };
      this.open2 = true;
    },
    /** 查询小程序企业走访打卡（银行自己上传功能）列表 */
    getList() {
      this.selectRadio = "";
      this.loading = true;
      listApiBankUser(this.queryParams).then((response) => {
        console.log(response, "表格数据");
        this.listApiBankUser = response.rows;
        this.listApiBankUser.forEach((e) => {
          e.loadIshow = false;
        });
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        bankName: null,
        isAuth: null,
        isAuthCount: null,
        initAuthCount: null,
        countType: false,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.bankNamedisabled = false;
      this.isAuthdisabled = false;
      this.isAuthCountdisabled = false;
      this.initAuthCountdisabled = false;
      (this.countTypeDisabled = false), this.reset();
      this.title = "新增";
      this.open = true;
    },
    /** 修改按钮操作 */
    handleUpdate() {
      console.log(this.checkTrue, "this.checkTrue");
      if (this.checkTrue.bankName) {
        this.bankNamedisabled = true;
        this.isAuthdisabled = false;
        this.isAuthCountdisabled = false;
        this.initAuthCountdisabled = false;
        (this.countTypeDisabled = false), this.reset();
        this.form = this.checkTrue;
        this.title = "修改";
        if (this.form.initAuthCount == -1) {
          this.$set(this.form, "countType", false);
        } else {
          this.$set(this.form, "countType", true);
        }

        this.open = true;
        console.log(this.form, "表单内容");
      }
    },
    /** 提交按钮 */
    submitForm() {
      console.log((this.title = "修改"));

      let parmas = JSON.parse(JSON.stringify(this.form));
      if (parmas.countType == false) {
        parmas.initAuthCount = -1;
      } else {
        parmas.initAuthCount = Number(parmas.initAuthCount);
      }
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.title == "新增") {
            console.log(parmas, "接口参数");
            addBankUser(parmas).then((response) => {
              if (response.code == 200) {
                this.$message.success("新增成功");
                this.$nextTick(() => {
                  this.getList();
                });
              } else {
                this.$message.error("新增失败");
              }
            });
          } else if (this.title == "修改") {
            console.log(parmas, "接口参数");
            updateBankUser(parmas).then((response) => {
              if (response.code == 200) {
                this.$message.success("修改成功");

                this.$nextTick(() => {
                  this.getList();
                });
              } else {
                this.$message.error("修改失败");
              }
            });
            this.open = false;
            this.$set(this.queryParams, "pageNum", 1);
          }
        }
      });
    },
    countChange() {
      this.$set(this.form, "initAuthCount", 0);
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm(
          '是否确认删除小程序企业走访打卡（银行自己上传功能）编号为"' +
            ids +
            '"的数据项？'
        )
        .then(function () {
          return delCLOCK(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "visitcorp/CLOCK/export",
        {
          ...this.queryParams,
        },
        `CLOCK_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style>
.isAuthTrue {
  background-color: #e8f4ff;
  border-color: #d1e9ff;
  color: #1890ff;
  display: inline-block;
  height: 32px;
  padding: 0 10px;
  line-height: 30px;
  font-size: 12px;
  color: #1890ff;
  border-width: 1px;
  border-style: solid;
  border-radius: 4px;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  white-space: nowrap;
}
.isAuthFalse {
  background-color: #ff9393;
  border-color: #ff9393;

  display: inline-block;
  height: 32px;
  padding: 0 10px;
  line-height: 30px;
  font-size: 12px;
  color: #fff;
  border-width: 1px;
  border-style: solid;
  border-radius: 4px;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  white-space: nowrap;
}
.kz_icon img {
  width: 25px;
  cursor: pointer;
}
.kz_icon2 img {
  width: 23px;
  cursor: pointer;
}
.loading_icon img {
  width: 25px;
  transform-origin: center;
  animation: rotate 2s linear infinite;
}
</style>
